package br.edu.ufcg.dsc.sig.geointerest.gui.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.GoogleBarAdsOptions;
import com.google.gwt.maps.client.GoogleBarOptions;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapOptions;
import com.google.gwt.maps.client.MapUIOptions;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.GoogleBarAdsOptions.AdSafeOption;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.event.MapRightClickHandler;
import com.google.gwt.maps.client.event.PolygonCancelLineHandler;
import com.google.gwt.maps.client.event.PolygonEndLineHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.overlay.Icon;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.maps.client.overlay.PolyStyleOptions;
import com.google.gwt.maps.client.overlay.Polygon;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class GeoInterest {
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GeoInterestServerAsync geoInterestServer = GWT
			.create(GeoInterestServer.class);

	String userLogin;
	private MapWidget map;

	Label mostrar = new Label("Consultas");
	CheckBox cinemaCB = new CheckBox("Cinemas");
	CheckBox escolaCB = new CheckBox("Escolas");
	CheckBox hospitalCB = new CheckBox("Hospitais");
	CheckBox hotelCB = new CheckBox("Hotéis");
	CheckBox locadoraCB = new CheckBox("Locadoras");
	CheckBox lojaCB = new CheckBox("Lojas");
	CheckBox museuCB = new CheckBox("Museus");
	CheckBox pontoTuristicoCB = new CheckBox("Pontos Turísticos");
	CheckBox restauranteCB = new CheckBox("Restaurantes");

	Polygon poly;

	Button editButton=new Button("Adicionar");
	Button removeButton=new Button("Remover");

	Label tituloEdicao = new Label();
	TextBox nomeTB = new TextBox();
	TextBox enderecoTB = new TextBox();
	ListBox tipoLB = new ListBox();

	TextBox telefoneTB = new TextBox();
	TextBox latitudeTB = new TextBox();
	TextBox longitudeTB = new TextBox();
	TextBox donoTB = new TextBox();
	TextBox descricaoTB = new TextBox();

	TextBox raioTB = new TextBox();
	ListBox notaLB = new ListBox();
	TextBox notaQueryTB = new TextBox();
	Button queryButton=new Button("Consultar");
	Button areaButton=new Button("Selecionar área");

	Button commentButton=new Button("Comentar");

	Label tituloAvaliar = new Label();	
	TextBox commentTB = new TextBox();
	TextBox notaTB = new TextBox();

	Marker lastEmptyOrAdd =  null;
	Marker lastEdit = null;

	boolean terminouDesenhar = true;

	private InfoWindow info = null;

	HashMap<Overlay, POI> markerToPOI = new HashMap<Overlay, POI>();

	public GeoInterest(String userLogin){
		geoInterestServer.getAllPOIs(new AsyncCallback<List<POI>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<POI> result) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		LatLng CampinaGrande = LatLng.newInstance(-7.230718,-35.881666);

		Panel mapPanel = new HorizontalPanel();

		map = new MapWidget(CampinaGrande, 14);
		map.setSize("640px", "520px");

	    MapUIOptions options = map.getDefaultUI();
	    options.setScrollwheel(false);
	    options.setDoubleClick(false);
	    options.setLargeMapControl3d(true);
	    map.setUI(options);

	    GoogleBarAdsOptions adsOptions = GoogleBarAdsOptions.newInstance();
	    adsOptions.setAdSafe(AdSafeOption.ADSAFE_MEDIUM);
	    GoogleBarOptions googleBarOptions = GoogleBarOptions.newInstance();
	    googleBarOptions.setAdsOptions(adsOptions);
	    googleBarOptions.setShowOnLoad(true);
	    googleBarOptions.setStyle("new");
	    MapOptions mapOptions = MapOptions.newInstance();
	    mapOptions.setGoogleBarOptions(googleBarOptions);
	    // map.setUIToDefault();
	    map.setGoogleBarEnabled(true);
	    
		//	    MapUIOptions options = MapUIOptions.newInstance(Size.newInstance(640, 480));
		//	    options.setScrollwheel(true);
		//	    map.setUI(options);
		map.addMapClickHandler(getViewPOIHandler());
		map.addMapRightClickHandler(getAddPOIHandler());

		VerticalPanel editPOIVP = new VerticalPanel();
		editPOIVP.add(createEditionForm());

		HorizontalPanel commentPanel = new HorizontalPanel();
		commentPanel.add(getCommentsWidget());
		commentPanel.setWidth("100%");
		RootPanel.get("Comment").add(commentPanel);

		VerticalPanel queryPanel = new VerticalPanel();
		queryPanel.add(getQueriesWidget());
		queryPanel.setHeight("100%");
		RootPanel.get("Query").add(queryPanel);


		mapPanel.add(editPOIVP);
		mapPanel.add(map);
		//mapPanel.add(queryPanel);		
		// Add the map to the HTML host page
		RootPanel.get("GeoInterest").add(mapPanel);

		//		Panel formContainer = new HorizontalPanel();
		//		formContainer.add(createAdvancedForm());
		//		RootPanel.get("PoiEdition").add(formContainer);

		
		
		this.userLogin = userLogin;
	}

	private Widget getQueriesWidget() {
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		layout.setWidth("300");
		layout.setHeight("720");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		//cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);


		final String[] listTypes = {">=", ">", "=", "!=", "<=", "<"};
		for (int i = 0; i < listTypes.length; i++) {
			notaLB.addItem(listTypes[i]);
		}
		


		// Add some standard form options	    
		tituloEdicao.setText("Adicionar ponto de interesse");
		//layout.setWidget(0, 0, mostrar);
		layout.setHTML(0, 1, "Consultas");
		layout.setHTML(1, 0, "Tipo");
		layout.setWidget(1, 1, cinemaCB);
		layout.setWidget(2, 1, escolaCB);
		layout.setWidget(3, 1, hospitalCB);
		layout.setWidget(4, 1, hotelCB);
		layout.setWidget(5, 1, locadoraCB);
		layout.setWidget(6, 1, lojaCB);
		layout.setWidget(7, 1, museuCB);
		layout.setHTML(8, 0, "Nota");
		layout.setWidget(8, 1, notaLB);
		layout.setWidget(9, 1, notaQueryTB);
		layout.setHTML(10, 0, "Raio");
		layout.setWidget(10, 1, raioTB);
		layout.setWidget(11, 1, areaButton);
		layout.setWidget(12, 1, queryButton);
		layout.setHTML(13, 0, "<br>");
		layout.setHTML(14, 0, "<br>");
		layout.setHTML(15, 0, "<br>");
		layout.setHTML(16, 0, "<br>");
		layout.setHTML(17, 0, "<br>");
		layout.setHTML(18, 0, "<br>");
		layout.setHTML(19, 0, "<br>");
		layout.setHTML(20, 0, "<br>");
		layout.setHTML(21, 0, "<br>");

		areaButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender)    {
				if(lastEmptyOrAdd != null){
					map.removeOverlay(lastEmptyOrAdd);
				}
				createPolygon();
			}
		});

		queryButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender)    {
				makeQuery();
			}
		});

		// Wrap the contents in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);
		decPanel.setHeight("100%");
		return decPanel;
	}

	private void makeQuery() {
		// TODO Auto-generated method stub

	}

	private Widget getCommentsWidget() {
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		layout.setWidth("940");
		layout.setHeight("200");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		cellFormatter.setHorizontalAlignment(0, 1,
				HasHorizontalAlignment.ALIGN_CENTER);
		cellFormatter.setVerticalAlignment(2, 0,
				HasVerticalAlignment.ALIGN_TOP);
		cellFormatter.setVerticalAlignment(2, 1,
				HasVerticalAlignment.ALIGN_TOP);
		cellFormatter.setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_RIGHT);

		// Add some standard form options	   
		tituloAvaliar.setText("Avaliar ponto de interesse");
		layout.setWidget(0, 1, tituloAvaliar);
		layout.setHTML(1, 0, "Nota");
		notaTB.setWidth("50");
		notaTB.setMaxLength(5);
		layout.setWidget(2, 0, notaTB);
		commentTB.setWidth("840");
		commentTB.setHeight("60");
		layout.setHTML(1, 1, "Comentário");
		layout.setWidget(2, 1, commentTB);
		layout.setWidget(3, 1, commentButton);

		/** Add a click listener which is called
		when the button is clicked */
		commentButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender)    {
				addComment();
			}


		});

		disableComments();

		// Wrap the contents in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);
		return decPanel;
	}

	private void addComment() {
		POI poi = markerToPOI.get(lastEdit);
		poi.avalia(userLogin, notaTB.getText(), commentTB.getText());
	}

	/**
	 * Create a form that contains undisclosed advanced options.
	 */
	private Widget createEditionForm() {
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		layout.setWidth("300");
		layout.setHeight("510");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		final String[] listTypes = {"Cinema", "Escola", "Hospital", "Hotel", "Locadora", "Loja", "Museu"};
		for (int i = 0; i < listTypes.length; i++) {
			tipoLB.addItem(listTypes[i]);
		}

		// Add some standard form options	    
		layout.setWidget(0, 0, tituloEdicao);
		layout.setHTML(1, 0, "Nome");
		layout.setWidget(1, 1, nomeTB);
		layout.setHTML(2, 0, "Endereço");
		layout.setWidget(2, 1, enderecoTB);
		layout.setHTML(3, 0, "Tipo");
		layout.setWidget(3, 1, tipoLB);
		layout.setHTML(4, 0, "Telefone");
		layout.setWidget(4, 1, telefoneTB);
		layout.setHTML(5, 0, "Latitude");
		layout.setWidget(5, 1, latitudeTB);
		layout.setHTML(6, 0, "Longitude");
		layout.setWidget(6, 1, longitudeTB);
		layout.setHTML(7, 0, "Usuário");
		layout.setWidget(7, 1, donoTB);
		descricaoTB.setHeight("200");
		layout.setHTML(8, 0, "Descrição");
		layout.setWidget(8, 1, descricaoTB);
		latitudeTB.setReadOnly(true);
		longitudeTB.setReadOnly(true);

		disableEdition();
		//Add the Login button to the form
		layout.setWidget(9,1, editButton );

		/** Add a click listener which is called
		when the button is clicked */
		editButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender)    {
				if(editButton.getHTML().equalsIgnoreCase("adicionar")){
					addPoi();
				} else {
					editPoi();
				}

			}
		});

		layout.setWidget(9,0, removeButton);
		/** Add a click listener which is called
		when the button is clicked */
		removeButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender)    {
				removePoi();
			}
		});

		removeButton.setVisible(false);

		// Wrap the contents in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);
		return decPanel;
	}


	private void removePoi() {
		POI poi = markerToPOI.remove(lastEdit);
		map.removeOverlay(lastEdit);
		clearEdition();
		disableEdition();
	}


	private void editPoi() {
		POI poi = markerToPOI.get(lastEdit);

		if(poi != null){
			poi.setNome(nomeTB.getText());
			poi.setEndereco(enderecoTB.getText());
			poi.setTipo(getTipo());
			poi.setTelefone(telefoneTB.getText());
			poi.setDescricao(descricaoTB.getText());
		}
	}

	private void addPoi() {
		Double latitude = new Double(latitudeTB.getText());
		Double longitude = new Double(longitudeTB.getText());
		POI poi = new POI(nomeTB.getText(), enderecoTB.getText(), getTipo(), telefoneTB.getText(), 
				descricaoTB.getText(), userLogin, latitude, longitude); 

		Marker poiMarker = new Marker(LatLng.newInstance(latitude, longitude));					

		markerToPOI.put(poiMarker, poi);
		map.addOverlay(poiMarker);
		map.removeOverlay(lastEmptyOrAdd);
		setEditMode(poiMarker);
	}

	private void enableAndClearEdition() {
		enableEdition();
		clearEdition();
	}

	private void enableEdition() {
		setEditionWritable();
		nomeTB.setEnabled(true);
		enderecoTB.setEnabled(true);
		tipoLB.setEnabled(true);
		descricaoTB.setEnabled(true);
		telefoneTB.setEnabled(true);
		editButton.setEnabled(true);
	}

	private void setEditionWritable(){
		nomeTB.setReadOnly(false);
		enderecoTB.setReadOnly(false);
		tipoLB.setEnabled(true);
		descricaoTB.setReadOnly(false);
		telefoneTB.setReadOnly(false);
		editButton.setEnabled(true);
		donoTB.setEnabled(true);
		donoTB.setReadOnly(true);
		latitudeTB.setEnabled(true);
		latitudeTB.setReadOnly(true);
		longitudeTB.setEnabled(true);
		longitudeTB.setReadOnly(true);
	}

	private void clearEdition() {
		nomeTB.setText("");
		enderecoTB.setText("");
		descricaoTB.setText("");
		telefoneTB.setText("");
		donoTB.setText("");
		tipoLB.setSelectedIndex(0);
	}

	private void setEditionReadOnly() {
		raioTB.setEnabled(false);
		raioTB.setText("");
		if(poly != null){
			map.removeOverlay(poly);
			poly = null;
		}
		nomeTB.setReadOnly(true);
		enderecoTB.setReadOnly(true);
		tipoLB.setEnabled(false);
		descricaoTB.setReadOnly(true);
		telefoneTB.setReadOnly(true);
		editButton.setEnabled(false);
		editButton.setHTML("Adicionar");
	}

	private void disableEdition() {
		raioTB.setEnabled(false);
		raioTB.setText("");
		if(poly != null){
			map.removeOverlay(poly);
			poly = null;
		}
		nomeTB.setEnabled(false);
		enderecoTB.setEnabled(false);
		tipoLB.setEnabled(false);
		descricaoTB.setEnabled(false);
		telefoneTB.setEnabled(false);
		donoTB.setEnabled(false);
		latitudeTB.setEnabled(false);
		longitudeTB.setEnabled(false);
		editButton.setEnabled(false);
		editButton.setHTML("Adicionar");
		removeButton.setVisible(false);
	}


	private void display(POI poi) {
		nomeTB.setText(poi.getNome());
		enderecoTB.setText(poi.getEndereco());
		tipoLB.setSelectedIndex(getIndex(poi.getTipo()));
		descricaoTB.setText(poi.getDescricao());
		donoTB.setText(poi.getDono());
		telefoneTB.setText(poi.getTelefone());
		latitudeTB.setText("" + poi.getLatitude());
		longitudeTB.setText("" + poi.getLongitude());
		String myComment = poi.getComment(userLogin);
		String minhaNota = poi.getNota(userLogin);
		if(myComment != null){
			commentTB.setText(myComment);
		}
		if(minhaNota != null){
			notaTB.setText(minhaNota);
		}
	}

	private int getIndex(String tipo) {
		final String[] listTypes = {"cinema", "escola", "hospital", "hotel", "locadora", "loja", "museu"};
		for (int i = 0; i < listTypes.length; i++) {
			if(tipo.equalsIgnoreCase(listTypes[i])){
				return i;
			}
		}
		return -1;
	}

	private MapClickHandler getViewPOIHandler() {
		return new MapClickHandler() {

			@Override
			public void onClick(MapClickEvent event) {

				if(terminouDesenhar){
					tituloEdicao.setText("Visualizar ponto de interesse");
					if(lastEmptyOrAdd != null){
						map.removeOverlay(lastEmptyOrAdd);
					}

					LatLng latlng = event.getLatLng();
					Overlay overlay = event.getOverlay();
					if (overlay != null && markerToPOI.containsKey(overlay)) {
						setEditionReadOnly();
						POI poi = markerToPOI.get(overlay);
						viewPoi(poi);
						lastEdit = (Marker)overlay;
						displayInfoWindow(lastEdit.getLatLng(), poi);
						enableComments();
					} else if(overlay == null || overlay instanceof Marker){
						clearEdition();
						disableEdition();
						disableComments();

						raioTB.setEnabled(true);

						if(overlay != null){
							latlng = ((Marker)overlay).getLatLng();
						}

						latitudeTB.setText("" + latlng.getLatitude());
						longitudeTB.setText("" + latlng.getLongitude());

						lastEmptyOrAdd = new Marker(latlng, getTargetMarkerOptions());
						map.addOverlay(lastEmptyOrAdd);
					}
				}


			}
		};
	}


	private void enableComments() {
		notaTB.setEnabled(true);
		commentTB.setEnabled(true);
		commentButton.setEnabled(true);
	}

	private void disableComments() {
		raioTB.setEnabled(false);
		raioTB.setText("");
		if(poly != null){
			map.removeOverlay(poly);
			poly = null;
		}
		notaTB.setText("");
		commentTB.setText("");
		notaTB.setEnabled(false);
		commentTB.setEnabled(false);
		commentButton.setEnabled(false);
	}

	private MapRightClickHandler getAddPOIHandler() {
		return new MapRightClickHandler() {


			public void onRightClick(MapRightClickEvent e) {

				if(terminouDesenhar){
					Overlay overlay = e.getOverlay();
					LatLng point = map.convertContainerPixelToLatLng(e.getPoint());	

					disableComments();

					if (overlay != null && overlay instanceof Marker && markerToPOI.containsKey(overlay)) {

						if(lastEmptyOrAdd != null){
							map.removeOverlay(lastEmptyOrAdd);
						}

						Marker marker = (Marker)overlay;
						POI poi = markerToPOI.get(marker);
						viewPoi(poi);

						if(poi.getDono().equals(userLogin)){
							setEditMode(marker);
							enableEdition();

						} else {
							tituloEdicao.setText("Visualizar ponto de interesse");
							setEditionReadOnly();
							enableComments();
							lastEdit = (Marker)overlay;
						}

						//TODO: EDITAR POI

					} else {
						enableAndClearEdition();

						tituloEdicao.setText("Adicionar ponto de interesse");
						editButton.setHTML("Adicionar");
						donoTB.setText(userLogin);
						latitudeTB.setText("" + point.getLatitude());
						longitudeTB.setText("" + point.getLongitude());

						if(lastEmptyOrAdd != null){
							map.removeOverlay(lastEmptyOrAdd);
						}

						lastEmptyOrAdd = new Marker(point, getAddPOIMarkerOptions());
						map.addOverlay(lastEmptyOrAdd);
					}
				}


			}



		};


	}


	private void setEditMode(Marker marker) {
		lastEdit = marker;
		editButton.setHTML("Editar");
		tituloEdicao.setText("Editar ponto de interesse");
		removeButton.setVisible(true);
	}

	private MarkerOptions getAddPOIMarkerOptions() {
		MarkerOptions opts = MarkerOptions.newInstance();
		Icon icon = Icon.newInstance("here.png");
		icon.setShadowURL("here-shadow.png");
		icon.setIconAnchor(Point.newInstance(8, 28));
		opts.setIcon(icon);
		return opts;
	}

	private MarkerOptions getTargetMarkerOptions() {
		MarkerOptions opts = MarkerOptions.newInstance();
		Icon icon = Icon.newInstance("target.png");
		icon.setIconAnchor(Point.newInstance(8, 24));
		opts.setIcon(icon);
		return opts;
	}

	private void viewPoi(POI poi) {
		tituloEdicao.setText("Visualizar ponto de interesse");
		display(poi);
	}

	private String getTipo() {

		switch (tipoLB.getSelectedIndex()) {
		case 0:
			return "cinema";
		case 1:
			return "escola";
		case 2:
			return "hospital";
		case 3:
			return "hotel";
		case 4:
			return "locadora";
		case 5:
			return "loja";
		default:
			return "museu";
		}

	}

	private String getOperator() {

		switch (notaLB.getSelectedIndex()) {
		case 0:
			return ">=";
		case 1:
			return ">";
		case 2:
			return "=";
		case 3:
			return "!=";
		case 4:
			return "<=";
		default:
			return "<";
		}

	}

	/**
	 * Display one of the info Window test cases.
	 */
	private void displayInfoWindow(LatLng latlng, POI poi) {

		// pop down the existing info window.
		if (info != null) {
			info.close();
		}

		info = map.getInfoWindow();

		String nota = "Não avaliado";

		int count = 0;
		double soma = 0;
		for (String umaNota : poi.getNotas()) {
			if(umaNota != null){
				try{
					soma += Double.parseDouble(umaNota);
					count++;
				} catch (NumberFormatException nfe){
					continue;
				}
			}

		}

		if(count > 0){
			nota = "" + soma/count;
		}

		InfoWindowContent content;


		// The default case
		Tree tree = new Tree();
		TreeItem avaliacao = new TreeItem("Nota: " + nota);
		tree.addItem(avaliacao);

		for (Entry<String, String> comment : poi.getComments()) {
			TreeItem commentator = new TreeItem("Comentario de " + comment.getKey());
			tree.addItem(commentator);
			TreeItem commentItem = new TreeItem(comment.getValue());
			commentator.addItem(commentItem);
		}

		//tree.setSize("217px", "104px");
		content = new InfoWindowContent(tree);

		info.open(latlng, content);
	}

	private void createPolygon() {

		terminouDesenhar = false;
		poly = new Polygon(new LatLng[0], "#FF0000", 1, 1.0, "#FF0000", 0.5);
		PolyStyleOptions style = PolyStyleOptions.newInstance("#FF0000", 2,  1.0);

		map.addOverlay(poly);
		poly.setDrawingEnabled();
		poly.setStrokeStyle(style);

		poly.addPolygonCancelLineHandler(new PolygonCancelLineHandler() {

			public void onCancel(PolygonCancelLineEvent event) {
				poly = null;
			}
		});

		poly.addPolygonEndLineHandler(new PolygonEndLineHandler() {

			public void onEnd(PolygonEndLineEvent event) {
				terminouDesenhar = true;
			}
		});
	}
}